package Day2025_02_25;

public class LeetCode38 {
    public String countAndSay(int n) {
        n--;
        String s = "1";
        int len = 0;
        while (n != 0) {
            char[] ss = s.toCharArray();
            len = ss.length;
            StringBuilder str = new StringBuilder();
            int left = 0, right = 0;
            char tmp = 0;
            for (left = 0, right = 0; right < len; right++) {
                tmp = ss[right];
                if (right < len - 1 && ss[right + 1] != ss[left]) {
                    str.append(right - left + 1);
                    str.append(tmp);
                    left = right + 1;
                }
            }
            str.append(right - left);
            str.append(tmp);
            s = str.toString();
            n--;
        }
        return s;
    }

    public static void main(String[] args) {
        LeetCode38 leetCode38 = new LeetCode38();
        System.out.println(leetCode38.countAndSay(4));
    }
}
